package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.AdministrativeOffice
import cn.exrick.xboot.base.mapper.AdministrativeOfficeMapper
import cn.exrick.xboot.base.service.IAdministrativeOfficeService
import cn.exrick.xboot.base.vo.AdministrativeOfficeVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.UserService
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import javax.annotation.Resource

/**
 * 科室接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IAdministrativeOfficeServiceImpl : ServiceImpl<AdministrativeOfficeMapper?, AdministrativeOffice?>(), IAdministrativeOfficeService {

    @Autowired
    private val securityUtil: SecurityUtil? = null

    @Autowired
    private lateinit var userservice: UserService


    override fun createOrUpdateOffice(administrativeOffice: AdministrativeOffice?): Result<Any?>? {

        return administrativeOffice?.run {

            administrativeOffice.areaId = securityUtil!!.currUser.areaId
            saveOrUpdate(administrativeOffice)
            ResultUtil.success<Any?>("操作成功")
        } ?: ResultUtil.error("操作失败")
    }

    override fun removeOffice(ids: Array<out String>): Result<Any?>? {

        removeByIds(ids.toList())
        return ResultUtil.success("删除成功！")
    }

    override fun getByPage(pageVo: PageVo, administrativeOffice: AdministrativeOffice?): Result<MutableMap<String, Any?>>? {

       return ToolUtil.setListToNul(list(KtQueryWrapper(AdministrativeOffice::class.java).eq(AdministrativeOffice::areaId, securityUtil!!.currUser.areaId)
               .like(ToolUtil.isNotEmpty(administrativeOffice?.officeName), AdministrativeOffice::officeName, administrativeOffice?.officeName)
                .orderByDesc(AdministrativeOffice::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = this.size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { AdministrativeOfficeVo().apply {

                ToolUtil.copyProperties(it, this)
                createBy = userservice.get(it?.createBy)?.nickname
            } })

            ResultUtil.data(result, "获取科室数据成功")
        } ?: ResultUtil.error(201,"暂无数据")
    }

    override fun getOfficeNameById(id: String?): String? {

        return id?.let { getById(it)?.officeName }
    }

    override fun officeVoList(): Result<MutableList<AdministrativeOfficeVo>?>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(AdministrativeOffice::class.java).eq(AdministrativeOffice::areaId, securityUtil!!.currUser.areaId)
                .orderByDesc(AdministrativeOffice::createTime)))?.run {

           ResultUtil.data( this.map {
               AdministrativeOfficeVo().apply { ToolUtil.copyProperties(it, this) }
           }.toMutableList())
        }?: ResultUtil.error(201,"暂无数据")
    }
}